package org.momo.rpc.provider.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.momo.rpc.constant.Constant;

/**
 * 服务注册元信息
 */
@Data
@AllArgsConstructor
public class ProviderRegistryInfo {

    private String serviceName;

    private String serviceVersion;

    private String host;

    private Integer port;

    public String getServiceKey() {
        return String.join(Constant.SERVICE_DELIMITER, serviceName, serviceVersion);
    }

    public String getServiceAddress() {
        return String.join(Constant.SERVICE_ADDRESS_DELIMITER, host, port.toString());
    }

    public static ProviderRegistryInfo convertToMetaInfo(String serviceName, String serviceVersion, String serviceAddress) {
        String[] info = serviceAddress.split(Constant.SERVICE_ADDRESS_DELIMITER);
        return new ProviderRegistryInfo(serviceName, serviceVersion, info[0], Integer.parseInt(info[1]));
    }

}
